﻿using Entity;
using ServiceCore;
using ServiceCore.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Customer.Bussiness_Service
{
    public interface Ilogin_service
    {
        ResultObjModel Login(LoginParams Params);
        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="Params"></param>
        /// <returns></returns>
        ResultObjModel LoginForWx(WxLoginParams Params);
    }
    public class login_service : Ilogin_service
    {
        private readonly IToole_DI _tool;
        public login_service(IToole_DI Tool)
        {
            _tool = Tool;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ResultObjModel Login(LoginParams Params)
        {
            ResultObjModel result = new ResultObjModel();
            try
            {
                //string errmsg = "";
                //if (!CheckPicCode(Params.CheckToken, Params.code, out errmsg))
                //{
                //    result.code = 1;
                //    result.msg = errmsg;
                //    return result;
                //}
                if (string.IsNullOrWhiteSpace(Params.username) || string.IsNullOrWhiteSpace(Params.pwd))
                {
                    result.code = 1;
                    result.msg = "用户名或密码不能为空";
                    return result;
                }
                //var LogUser = _tool.di_db.GetModel<sys_user>(new string[] { "login_name=@login_name" }, new { login_name = Params.username });
                var LogUser = _tool.di_db.GetList<rk_user>("select * from rk_user where login_name=@login_name", new { login_name = Params.username }).FirstOrDefault();
                if (LogUser == null)
                {
                    result.code = 1;
                    result.msg = "登录用户不存在";
                    return result;
                }
                else if (!LogUser.isuse)
                {
                    result.code = 1;
                    result.msg = "用户已被禁用";
                    return result;
                }
                else
                {
                    string CheckPwd = SecurityToMd5.Start(Params.pwd + "^_^");
                    if (CheckPwd == LogUser.login_pwd)
                    {
                        _tool.di_db.do_Execute("update rk_user set lastLoginTime=@lastLoginTime,lastLoginIP=@lastLoginIP where uid=@uid", new { lastLoginTime = DateTime.Now, lastLoginIP = _tool.get_Ip, uid = LogUser.uid });
                        //登录成功
                        var dic = new Dictionary<string, object>();
                        dic.Add("tokenuser", LogUser);
                        var token = JWT.Jwt.Create(dic);
                        result.code = 0;
                        result.msg = "登录成功";
                        result.data = new { token = token,  wx_name = LogUser.u_name, LogUser.role_id, LogUser.headimg };
                        return result;
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "用户名或密码不正确";
                        return result;
                    }
                }
            }
            catch (Exception ex)
            {
                result.code = 1;
                result.msg = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ResultObjModel LoginForWx(WxLoginParams Params)
        {
            ResultObjModel result = new ResultObjModel();
            try
            {
                //string errmsg = "";
                //if (!CheckPicCode(Params.CheckToken, Params.code, out errmsg))
                //{
                //    result.code = 1;
                //    result.msg = errmsg;
                //    return result;
                //}
                if (string.IsNullOrWhiteSpace(Params.openid))
                {
                    result.code = 1;
                    result.msg = "登录参数不完整";
                    return result;
                }
                //var LogUser = _tool.di_db.GetModel<sys_user>(new string[] { "login_name=@login_name" }, new { login_name = Params.username });
                var LogUser = _tool.di_db.GetList<rk_user>("select * from rk_user where bindOpenid=@bindOpenid", new { bindOpenid = Params.openid }).FirstOrDefault();
                if (LogUser == null)
                {
                    result.code = 1;
                    result.msg = "扫描微信号未开通";
                    return result;
                }
                else if (!LogUser.isuse)
                {
                    result.code = 1;
                    result.msg = "用户已被禁用";
                    return result;
                }
                else
                {
                    _tool.di_db.do_Execute("update rk_user set lastLoginTime=@lastLoginTime,lastLoginIP=@lastLoginIP where uid=@uid", new { lastLoginTime = DateTime.Now, lastLoginIP = _tool.get_Ip, uid = LogUser.uid });
                    //登录成功
                    var dic = new Dictionary<string, object>();
                    dic.Add("tokenuser", LogUser);
                    var token = JWT.Jwt.Create(dic);
                    result.code = 0;
                    result.msg = "登录成功";
                    result.data = new { token = token, wx_name = LogUser.u_name, LogUser.role_id, LogUser.headimg };
                    return result;
                }
            }
            catch (Exception ex)
            {
                result.code = 1;
                result.msg = ex.Message;
            }
            return result;
        }
    }  
}
